/**
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package spring.cloud.tasks.tasks_manager.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 获取真实本机网络的实现类.
 */
public class LocalHostService {

    private static final Logger log = LoggerFactory.getLogger(LocalHostService.class);
    private static final String IP_REGEX = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
            + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
            + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
    public static volatile String cachedIpAddress;

    /**
     * for inner test
     */

    /**
     * ip读取顺序：参数 -> 环境变量 -> bond0 -> eth0。
     */
    static {
        cachedIpAddress = System.getProperty("VIP_SATURN_RUNNING_IP", System.getenv("VIP_SATURN_RUNNING_IP"));
        if (StringUtils.isEmpty(cachedIpAddress)) {
            try {
                InetAddress inetAddress = InetAddress.getLocalHost();
                if (inetAddress.getHostAddress() == null || "127.0.0.1".equals(inetAddress.getHostAddress())) {
                    NetworkInterface ni = NetworkInterface.getByName("bond0");
                    if (ni == null) {
                        ni = NetworkInterface.getByName("eth0");
                    }
                    if (ni == null) {
                        throw new Exception("failed in getting ip from local host, bond0 and eth0");
                    }

                    Enumeration<InetAddress> ips = ni.getInetAddresses();
                    while (ips.hasMoreElements()) {
                        InetAddress nextElement = ips.nextElement();
                        if ("127.0.0.1".equals(nextElement.getHostAddress()) || nextElement instanceof Inet6Address
                                || nextElement.getHostAddress().contains(":")) {
                            continue;
                        }
                        inetAddress = nextElement;
                        break;
                    }
                }
                cachedIpAddress = inetAddress.getHostAddress();
            } catch (Throwable e) {
                log.error(
                        "[localHostService error][please configure hostname or bond0 or eth0. System is shutting down.]",
                        e);
                System.exit(-1);
            }
        } else {
            if (!isIpv4(cachedIpAddress)) {
                log.error("IP address {} is illegal. System is shutting down.", cachedIpAddress);
                System.exit(-1);
            }
        }
        log.info("Done initial localhostip: {}.", cachedIpAddress);
    }

    /**
     * @deprecated for test only!
     */
    @Deprecated
    public static void setCachedIpAddress(String ip) {
        cachedIpAddress = ip;
    }

    /**
     * @deprecated for test only!
     */

    public static void setCachedHostName(String hostName) {
    }

    private static boolean isIpv4(String ipAddress) {
        Pattern pattern = Pattern.compile(IP_REGEX);
        Matcher matcher = pattern.matcher(ipAddress);
        return matcher.matches();

    }
}
